package com.ChainBizPro.system.service.impl;

import com.ChainBizPro.system.common.ErrorCode;
import com.ChainBizPro.system.exception.BusinessException;
import com.ChainBizPro.system.mapper.StockMapper;
import com.ChainBizPro.system.model.domain.Stock;
import com.ChainBizPro.system.model.domain.StockTransactions;
import com.ChainBizPro.system.model.dto.stock.StockAddRequest;
import com.ChainBizPro.system.model.dto.stock.StockQueryRequest;
import com.ChainBizPro.system.service.StockService;
import com.ChainBizPro.system.service.StockTransactionsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author liusijia
 * @description 针对表【stock(库存)】的数据库操作Service实现
 * @createDate 2024-11-16 16:34:17
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Resource
    @Lazy
    private StockService stockService;

    @Resource
    private StockTransactionsService stockTransactionsService;

    @Override
    public Page<Stock> getQueryWrapper(Page<Stock> pageParam, StockQueryRequest stockQueryRequest) {
        if (stockQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();

        addLikeCondition(queryWrapper, "productName", stockQueryRequest.getProductName());

        if (stockQueryRequest.getQuantity() != null && stockQueryRequest.getQuantity() != 0) {
            queryWrapper.like("quantity", stockQueryRequest.getQuantity());
        }
        if (stockQueryRequest.getUnitPrice() != null) {
            queryWrapper.like("unitPrice", stockQueryRequest.getUnitPrice());
        }
        if (stockQueryRequest.getCreateTime() != null) {
            queryWrapper.like("createTime", stockQueryRequest.getCreateTime());
        }

        queryWrapper.orderByDesc("productId");

        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public boolean newStock(StockAddRequest stockAddRequest, HttpServletRequest request) {
        try {
            Stock stock = new Stock();
            QueryWrapper<Stock> queryStock = new QueryWrapper<>();
            queryStock.eq("productName", stockAddRequest.getProductName());
            if (stockService.getOne(queryStock) != null) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品已存在");
            }
            // 复制请求参数到实体对象
            BeanUtils.copyProperties(stockAddRequest, stock);
            // 保存到数据库
            boolean result = this.save(stock);
            // 创建库存交易记录
            StockTransactions stockTransactions = new StockTransactions();
            stockTransactions.setProductId(stock.getProductId());
            stockTransactions.setTransactionType("in");
            stockTransactions.setQuantity(stockAddRequest.getQuantity());
            stockTransactionsService.save(stockTransactions);
            return result;
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "系统错误");
        }
    }

    private void addLikeCondition(QueryWrapper<Stock> queryWrapper, String column, String value) {
        if (!StringUtils.isEmpty(value)) {
            queryWrapper.like(column, value);
        }
    }

}




